package com.koc.robot;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLES11;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.MotionEvent;

import com.koc.robot.util.GLResourceHelper;

public class BasicRenderer implements GLSurfaceView.Renderer
{
	public BasicRenderer(Context context)
	{
		mContext = context;
		
		m_bNextDeltaTimeZero = true;
		
		mFPSTickTime = 0f;
		mFPS = 0;
		mFPSTick = 0;
	}
	
	public void onDrawFrame(GL10 glUnused)
	{
		// calculate "global" dt
		calculateDeltaTime();
		
		// update the scheduler
		Scheduler.instance().tick(m_fDeltaTime);
		
		GLES11.glClear(GLES11.GL_COLOR_BUFFER_BIT | GLES11.GL_DEPTH_BUFFER_BIT);
		
		Scheduler.instance().render();
	}
	
	public void onSurfaceChanged(GL10 glUnused, int width, int height)
	{
		GLES11.glViewport(0, 0, width, height);

		Global.devicewidth = width;
		Global.deviceheight = height;

        GLES11.glMatrixMode(GLES11.GL_PROJECTION);
        GLES11.glLoadIdentity();
        GLES11.glOrthof(0, Global.screenwidth, Global.screenheight, 0, -1.0f, 1.0f);
        
        GLES11.glMatrixMode(GLES11.GL_MODELVIEW);
        GLES11.glLoadIdentity();
	}
	
	public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
	{
		initOpenGL();
		
		Global.context = mContext;
		
		GLResourceHelper.sharedHelper().reloadResources();
	}
	
	private void initOpenGL()
	{ 
		// Enable blending
		GLES11.glEnable(GLES11.GL_BLEND);
		
		// "Normal" blending
		GLES11.glBlendFunc(GLES11.GL_SRC_ALPHA, GLES11.GL_ONE_MINUS_SRC_ALPHA);
		
		GLES11.glDisable(GLES11.GL_DEPTH_TEST);
		
		GLES11.glDisableClientState(GLES11.GL_COLOR_ARRAY);
		
		// Enable line/point smoothing
		GLES11.glEnable(GLES11.GL_POINT_SMOOTH);
		GLES11.glHint(GLES11.GL_POINT_SMOOTH_HINT, GLES11.GL_NICEST);
		
		GLES11.glEnable(GLES11.GL_TEXTURE_2D);
		
		// Set pixel row alignment
		GLES11.glPixelStorei(GLES11.GL_UNPACK_ALIGNMENT, 2);
	}
	
	private void calculateDeltaTime()
	{
		long now = System.currentTimeMillis();
		
		// new delta time
		if (m_bNextDeltaTimeZero)
		{
			m_fDeltaTime = 0;
			m_bNextDeltaTimeZero = false;
		}
		else
		{
			m_fDeltaTime = (now - m_lastTickTime) / 1000.0f;
			m_fDeltaTime = Math.max(0, m_fDeltaTime);
		}
		
//		Log.d("Delta time", String.valueOf(m_fDeltaTime));
		
		m_lastTickTime = now;
		
		//tick FPS
		mFPSTick++;
		mFPSTickTime += m_fDeltaTime;
		if(mFPSTickTime > 1.0f)
		{
			mFPS = (int)(mFPSTick / mFPSTickTime);	//really, can treat FPSTick as FPS
			mFPSTickTime = 0;
			mFPSTick = 0;
			Log.d("FPS", String.valueOf(mFPS));
		}
	}
	
	public void onTouched(MotionEvent e)
	{
		Scheduler.instance().onTouched(e);
	}
	
	public void onButtonClicked(int buttonId)
	{
		Scheduler.instance().onButtonClicked(buttonId);
	}
	
	private Context mContext;
	
	private long m_lastTickTime;
	private float m_fDeltaTime;
	private boolean m_bNextDeltaTimeZero;
	
	private float mFPSTickTime;
	private int mFPS;
	private int mFPSTick;
}
